//
//  MainTabBar.swift
//  SwiftDemo
//
//  Created by hfzhangzhang on 2018/7/12.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

import UIKit

protocol MainTabBarDelegate {
    func barButtonAction(sender: UIButton)
}




class Animal{
    var age:Int  //没有初始化
    init(){
        self.age = 10
    }
    init(age:Int){
        self.age = age
    }
}


class Dog:Animal{
    var name:String
    override init(){
        self.name = "abc" // 1.先初始化本类
        super.init()  // 2.再初始化父类
        self.age+=1
        
    }
    init(age:Int, name:String) {
        self.name = name
        super.init(age: age)
    }
}



class MainTabBar: UITabBar {

    var mainTabBardelegate: MainTabBarDelegate?
    
    var centerButton:UIButton = {
        let centerButton = UIButton()
        centerButton.backgroundColor = UIColor.red
        centerButton.setImage(UIImage(named: "tabbar_add"), for:.normal)
        centerButton.setImage(UIImage(named: "tabbar_add"), for:.highlighted)
        centerButton.addTarget(self, action: #selector(addButtonClick), for: .touchUpInside)
        return centerButton
    }()
    
    @objc private func addButtonClick(){
        
        self.mainTabBardelegate?.barButtonAction(sender: self.centerButton);
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.isUserInteractionEnabled = true;
        self.backgroundColor = UIColor.blue;
        self.addSubview(centerButton)
    }

    override func layoutSubviews() {
        super.layoutSubviews();
        let centerbtnW = centerButton.currentImage!.size.width
        let centerbtnH = centerButton.currentImage!.size.height
        centerButton.frame = CGRect(x: (self.bounds.width-centerbtnW)/CGFloat(self.items!.count)*2, y: -14, width:centerbtnW, height:centerbtnH)
        let itemW:CGFloat = (self.frame.size.width-centerbtnW) / CGFloat(self.items!.count) ;
        
        var itemoffSet:CGFloat = 0;
        var index = 0;
        
        for item in self.subviews {
            if(item.isKind(of:NSClassFromString("UITabBarButton")!)){
                item.frame = CGRect(x: itemoffSet, y: 0, width: itemW, height: self.bounds.height)
               itemoffSet += itemW
                index += 1
                if(index==2){
                    itemoffSet += centerbtnW
                }
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if (self.isHidden == false) {
            let newPoint = self.convert(point, to: self.centerButton);
            if(self.centerButton.point(inside: newPoint, with: event)){
                return self.centerButton
            }
            else {
                return super.hitTest(point, with: event)
            }
        }
        else{
            return super.hitTest(point, with: event)
        }
    }
    

    
    
    

}
